package com.bgs.controller;

import com.bgs.pojo.*;
import com.bgs.service.UserService;
import com.bgs.tool.AliOss;
import com.bgs.tool.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @program: xkgc
 * @description:
 * @author: Mr.Zhao
 * @create: 2022-05-13 13:55
 */
@RestController
@RequestMapping("/zsl")
public class UserController {

    Logger log= LogManager.getLogger();

    @Autowired
    private UserService userService;

    @Autowired
    private AliOss aliOss;


    String file1=null;
    String file2=null;


    @GetMapping("/findAllUser")
    public R findAllUser(@RequestParam(defaultValue = "") String userName,@RequestParam(defaultValue = "") String phone,@RequestParam(defaultValue = "") Integer storeId){

      List<UserFind> list= userService.findAllUser(userName,phone,storeId);


        return  R.ok().data("records",list);
    }
    @PostMapping("/updatedUser")
    public R updatedUser(@RequestBody User user){


     int a=   userService.updatedUser(user);
    if (a>0){
        return  R.ok();
    }else {
        return  R.error();
    }


    }

    @PostMapping("/addUser")
    public R addUser(@RequestBody User user){

        int a=   userService.addUser(user);
        if (a>0){
            return  R.ok();
        }else {
            return  R.error();
        }


    }

    @GetMapping("/useCount/{storeId}")
    public R useCount(@PathVariable Integer storeId){

        int a=   userService.useCount(storeId);
        if (a>0){
            return  R.ok().data("count",a);
        }else {
            return  R.error();
        }


    }

    @GetMapping("/findAllAchieve/{userId}")
    private R findAllAchieve(@PathVariable Integer userId){

        List<Achievement> list= userService.findAllAchieve(userId);


        return  R.ok().data("yeji",list);



    }

    @RequestMapping("/addLogo")
    public String addLogo(MultipartFile file){

        file1=aliOss.upload(file);
        return file1;
    }

    @RequestMapping("/addPic")
    public String addPic(MultipartFile file){

        file2=aliOss.upload(file);
        return file2;
    }


    @PostMapping("/addStore")
    public R addStore(@RequestBody AddStore addStore){



        addStore.setPicture1(file1);
        addStore.setPicture2(file2);

        String lnglat = addStore.getLnglat();
        String[] split = lnglat.split(",");
        addStore.setLongitude(split[0]);
        addStore.setDimensionality(split[1]);

        System.out.println("------------------"+addStore.toString());
       int a=userService.addStore(addStore);
        if (a==1){

            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存成功，信息有误");
        }
    }



}
